<?php

/**
 * O2O团购PC版本
 * ============================================================================
 * * 版权所有 2009-2016 郑州司科计算机科技有限公司，并保留所有权利。
 * 购买网址: http://www.jzphp.com；
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和
 * 使用；不允许对程序代码以任何形式任何目的的再发布。
  * 咨询电话：400-823-0371 QQ：313999875
 * ============================================================================
 * $Author: zzsike $
 * $Time 2016-02-03 sammy $
*/


require_once(dirname(dirname(__FILE__)) . '/appwap.php');

$lat=$_SESSION['mylat'];
$lon=$_SESSION['mylng'];
$daytime = time();

$cates=DB::LimitQuery('category',array(
		'condition'=>array(
		'zone'=>'partner',
		'display'=>'Y',
		),
	'order'=>'order by sort_order desc,id asc',
));

$condition = array('open' => 'Y',);
$group_id = abs(intval($_GET['gid']));
if ($group_id) $condition['group_id'] = $group_id;
$sub_id  =  abs(intval($_GET['sid'])); 
$area_id =  abs(intval($_GET['aid']));
if ($area_id) $condition['area_id'] = $area_id;


$order_id = abs(intval($_GET['orderid']));
if ($order_id==0){
	$order="ORDER BY head DESC, id DESC";
}
if ($order_id==1){
	$order= 'ORDER BY ACOS(SIN(('.$lat.' * 3.1415) / 180 ) *SIN((latitude * 3.1415) / 180 ) +COS(('.$lat.' * 3.1415) / 180 ) * COS((latitude * 3.1415) / 180 ) *COS(('.$lon.'* 3.1415) / 180 - (longitude * 3.1415) / 180 ) ) * 6378.137 asc, head DESC, id DESC';
}
if ($order_id==2){
	$comment_good = Table::Count('order', $condition);
	$order="ORDER BY comment_good desc, head DESC, id DESC";
}
if ($order_id==4){
	$order="ORDER BY create_time asc, head DESC, id DESC";
}


if (option_yes('citypartner') && ($cid=abs(intval($city['id']))) ) {
	$condition['city_id'] = $cid;
}

if($group_id) $condition['group_id']=$group_id;
if($sub_id) $condition['sub_id']=$sub_id;
if($area) $condition['area']  = $area;

$count = Table::Count('partner', $condition);
list($pagesize, $offset, $pagestring) = pagestring($count, 30);
$partners = DB::LimitQuery('partner', array(
	'condition' => $condition,
	'order' => $order,
	'size' => $pagesize,
	'offset' => $offset,
));
foreach($partners AS $id=>$one){
	team_state($one);
	if ($one['state']=='none') $one['picclass'] = 'isopen';
	if ($one['state']=='soldout') $one['picclass'] = 'soldout';
	$one['comment_num'] = ($one['comment_good']+$one['comment_bad']+$one['comment_none']);
	$one['reputation'] = ($one['comment_num']>0)? moneyit(number_format(100*($one['comment_good']/$one['comment_num']), 2)) : null;
	$partners[$id] = $one;
}

$i=0;
foreach($partners as $val){
	$condition_team_c = array( 
			'partner_id' => $val['id'],
			"begin_time <  {$daytime}",
			//'team_top'=>1,
			'OR' => array(
				"now_number >= min_number",
				"end_time > {$daytime}",
				),      
			);
	$team_cc = DB::LimitQuery('team', array(
				'condition' => $condition_team_c,
				'order' => 'ORDER BY begin_time DESC, id DESC',
				));
	$team_count = count($team_cc);
	$tteam = DB::LimitQuery('category', array(
				'condition' => array( 'zone' => 'partner','id' => $val['group_id'],'display' => 'Y' ),
				));
				
	$ttteam = DB::LimitQuery('category', array(
				'condition' => array( 'zone' => 'area','id' => $val['area'],'display' => 'Y' ),
				));				
    if(!$ttteam){	   
	   $ttteam[0]['name'] = 'undefinition';	   
    }	
  $partners[$i]['tteam']  = $tteam[0]['name'];
  $partners[$i]['ttteam'] = $ttteam[0]['name']; 
  if($team_count){
    $partners[$i]['tuan']  = '<div class="part_markx"></div>';
  } 
  $comments_num = ($val['comment1'] + $val['comment2'] + $val['comment3'] + $val['comment4'] + $val['comment5']);
  $comments_fenshu =($val['comment1']*1+$val['comment2']*2+$val['comment3']*3+$val['comment4']*4+$val['comment5']*5)/$comments_num;
  $comments_baifenbi  =  $comments_fenshu*20;
  $partners[$i]['zhishu'] = $comments_num;
  $partners[$i]['comment_baifenbi']  =  $comments_baifenbi;
  $i++;
}

/*区域列表*/
$area_list = DB::LimitQuery('category', array(
	'condition'=>array(
		  'zone'=>'area', 
		  'fid'=>$city[id], 
		  'display'=>'Y',
		  ), 
		'order' => 'ORDER BY display ,sort_order DESC, id DESC',
));

/* now_comments */
$now_cc = array(
	'state' => 'pay',
	'comment_display' => 'Y',
	'comment_time > 0',
	'partner_id > 0',
);
$now_comments = DB::LimitQuery('order', array(
	'condition' => $now_cc,
	'order' => 'ORDER BY comment_time DESC',
));
foreach($now_comments AS $k=>$v) {
		$v['grade'] = 'A';
		$v['grade'] = $v['comment_grade']=='none' ? 'P' : $v['grade'];
		$v['grade'] = $v['comment_grade']=='bad' ? 'F' : $v['grade'];
		$v['comment'] = htmlspecialchars($v['comment_content']);
		$v['timespan'] = $daytime - $v['comment_time'];
		$now_comments[$k] = $v;
}

$partner_ids = Utility::GetColumn($now_comments, 'partner_id');
$cpartners = Table::Fetch('partner', $partner_ids);

$user_ids = Utility::GetColumn($now_comments, 'user_id');
$users = Table::Fetch('partner', $user_ids);
/* end */

$category = Table::Fetch('category', $group_id);
$pagetitle = '品牌商户';
$current_partner = 'class="current"';

function getdistance($lat1,$lng1,$lat2,$lng2){ 
	$radLat1 = deg2rad($lat1);
	$radLat2 = deg2rad($lat2);
	$radLng1 = deg2rad($lng1);
	$radLng2 = deg2rad($lng2);
	$a = $radLat1 -$radLat2;
	$b = $radLng1 -$radLng2;
	$s=2 * asin(sqrt(pow(sin($a/2),2) +cos($radLat1) * cos($radLat2) * pow(sin($b/2),2))) * 6378.137;
	return $s;
}

include template('wap_partner_index');
